package com.two.shopping.service.serviceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.two.shopping.mapper.IAdminMapper;
import com.two.shopping.pojo.Commodity;
import com.two.shopping.pojo.PageBean;
import com.two.shopping.service.IAdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class AdminServiceImpl implements IAdminService {
    @Autowired
    private IAdminMapper adminMapper;

    @Override
    public PageBean allList(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Commodity> list = adminMapper.AllList();
        Page<Commodity> p = (Page<Commodity>) list;
        //封装至PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public void delete(Integer id) {
        adminMapper.deleteId(id);
    }

    @Override
    public void deletes(List<Integer> id) {
        adminMapper.deletes(id);
    }

    @Override
    public void update(Commodity commodity) {
        commodity.setUpdateTime(LocalDateTime.now());
        commodity.setCreateTime(LocalDateTime.now());
        log.info(" update(Commodity commodity) {}",commodity);
        adminMapper.update(commodity);
    }

    @Override
    public PageBean pagelike(Integer page, Integer pageSize, String name, Integer state, Integer commodityTypeId) {
        PageHelper.startPage(page, pageSize);
        List<Commodity> list = adminMapper.pageLike(name, state, commodityTypeId);
        Page<Commodity> p = (Page<Commodity>) list;
        //封装至PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;

    }

    @Override
    public void add(Commodity commodity) {
        commodity.setCreateTime(LocalDateTime.now());
        commodity.setUpdateTime(LocalDateTime.now());
        log.info("commodit,{}", commodity);
        adminMapper.add(commodity);
    }
}
